package com.jbm.jbmsupplier.zp.liaotian;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.AllActivityApp;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.zp.adapters.Recent_ContactsAdapter;
import com.jbm.jbmsupplier.zp.entity.Recent_ContactsData;
import com.jbm.jbmsupplier.zp.presenters.RecentActivitys;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.jbmsupplier.zp.views.RecentViews;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis.GetInstan;
import static com.jbm.jbmsupplier.zp.utils.TableName.GROUD;
import static com.jbm.jbmsupplier.zp.utils.TableName.RECENT;


/**
 * Created by donar_work on 2017/4/5.
 */

public class Recent_ContactsActivity extends AllActivityApp implements RecentViews {

    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.toolbar_image)
    ImageView toolbarImage;
    private ArrayList<Recent_ContactsData> lists;
    private ClassUser classUser;
    private Recent_ContactsAdapter mAdapter;
    private ListView Lv;
    private boolean ISonResume = true;
    private RecentActivitys recentActivitys;

    @Override
    public int setContentView() {
        return R.layout.recent_contacts;
    }

    @Override
    public void initIalizeView() {
        ButterKnife.inject(this);
        ExitActivity.getInstance().addActivity(this);
        Lv = (ListView) findViewById(R.id.lv_msgList);
        recentActivitys = new RecentActivitys(this, this);
        recentActivitys.InitData();
        tetitle.setText("消息");
        toolbarImage.setVisibility(View.VISIBLE);
        Picasso.with(this).load(R.mipmap.tongxunlu).into(toolbarImage);
        Lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Recent_ContactsData toids = (Recent_ContactsData) parent.getAdapter().getItem(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("mais", toids);
                intent.putExtras(bundle);
                intent.setClass(Recent_ContactsActivity.this, MainActivity.class);
                startActivityForResult(intent, 100);
            }
        });
        Lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Recent_ContactsData idso = (Recent_ContactsData) arg0.getAdapter().getItem(arg2);
                if (idso.getTag() == 1) {
                    GetInstan(Recent_ContactsActivity.this).setdelete(GROUD, idso.getCgid(), classUser.getLogin());
                } else {
                    GetInstan(Recent_ContactsActivity.this).deletedata(RECENT, String.valueOf(idso.getTo_id()), classUser.getLogin());
                }
                Intent intent = new Intent();
                intent.putExtra("messageFlag", 50);
                intent.setAction("DropContactss");
                sendBroadcast(intent);
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("main", "onResume");
        //初始化数据加在注册广播
        recentActivitys.regisBroadcard();
    }


    @Override
    protected void onDestroy() {
        Log.e("main", "onDestroy");
        super.onDestroy();
    }


    @Override
    public void LoadData(final ArrayList<Recent_ContactsData> list2, final ClassUser classUser) {
        this.classUser = classUser;
        this.lists = list2;
        mAdapter = new Recent_ContactsAdapter(this, list2);
        Lv.setAdapter(mAdapter);
        recentActivitys.RequestChat();
    }

    @Override
    public void Updata(final ArrayList<Recent_ContactsData> list2, int tag) {

        if (tag == 1) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mAdapter == null) {
                        mAdapter = new Recent_ContactsAdapter(Recent_ContactsActivity.this, list2);
                        Lv.setAdapter(mAdapter);
                    } else {
                        mAdapter.notifyData(list2);
                    }
                }
            });
        }
    }

    @OnClick({R.id.teleft, R.id.toolbar_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                recentActivitys.OnStop();
               finish();
                break;
            case R.id.toolbar_image:
                startActivity(new Intent(this,TestDuiHuaActivity.class));
                break;
        }
    }
}
